package per.dg.jingchess.views.jingchess;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import per.dg.jingchess.R;

public class JingChessManager {
    
    Activity parent;

//    游戏模式 0：ai模式 1 : 和朋友一起
    int mode;

//    user选择的棋子外观 0 ： 圈圈  1 ：叉叉
    int selectChessman;

//    当前要走棋的一方 0 ：user 1 ： ai或者friends
    int playing;

//    游戏是否开始
    boolean started;

//    游戏结果 0 ：未结束 1 ： user赢了  2 ：ai或者friends赢了 3 ： 平局
    int gameOver;

    TextView tvOne, tvTwo, tvThree, tvFour, tvFive, tvSix, tvSeven, tvEight, tvNine;
    ImageView imgOne, imgTwo, imgThree, imgFour, imgFive, imgSix, imgSeven, imgEight, imgNine;

//    棋局，二维数组，3*3，0表示没有下子，1表示user下子，2表示ai或者friends下子
    int[][] chessData;

//    代表棋子的九个view
//    TextView[][] chessViews;
    ImageView[][] chessViews;

    JingChessListener jingChessListener;

//    View.OnClickListener onClickListener = new View.OnClickListener() {
//        @Override
//        public void onClick(View v) {
//
//            CharSequence text = ((TextView) v).getText();
//            if(text != null){
//                if(text.equals("1") || text.equals("2")){
//                    return;
//                }
//            }
//            switch (v.getId()){
//                case R.id.tv_one:
//                    chessViews[0][0].setText("2");
//                    chessData[0][0] = 2;
//                    break;
//                case R.id.tv_two:
//                    chessViews[1][0].setText("2");
//                    chessData[1][0] = 2;
//                    break;
//                case R.id.tv_three:
//                    chessViews[2][0].setText("2");
//                    chessData[2][0] = 2;
//                    break;
//                case R.id.tv_four:
//                    chessViews[0][1].setText("2");
//                    chessData[0][1] = 2;
//                    break;
//                case R.id.tv_five:
//                    chessViews[1][1].setText("2");
//                    chessData[1][1] = 2;
//                    break;
//                case R.id.tv_six:
//                    chessViews[2][1].setText("2");
//                    chessData[2][1] = 2;
//                    break;
//                case R.id.tv_seven:
//                    chessViews[0][2].setText("2");
//                    chessData[0][2] = 2;
//                    break;
//                case R.id.tv_eight:
//                    chessViews[1][2].setText("2");
//                    chessData[1][2] = 2;
//                    break;
//                case R.id.tv_nine:
//                    chessViews[2][2].setText("2");
//                    chessData[2][2] = 2;
//                    break;
//            }
//
//            if(isGameOver() == 0){
//                if(mode == 0){
//
//                }
//            }
//        }
//    };
    
    public JingChessManager(Activity activity, int mode, int selectChessman){
        parent = activity;
        this.mode = mode;
        this.selectChessman = selectChessman;
        initChess();
    }

    private void initChess(){
        initView();
        initData();
        setChessViewsEnable(false);
    }
    
    private void initView(){
//        tvOne = parent.findViewById(R.id.tv_one);
        imgOne = parent.findViewById(R.id.img_one);
//        tvOne.setOnClickListener(onClickListener);
//        tvTwo = parent.findViewById(R.id.tv_two);
        imgTwo = parent.findViewById(R.id.img_two);
//        tvTwo.setOnClickListener(onClickListener);
//        tvThree = parent.findViewById(R.id.tv_three);
        imgThree = parent.findViewById(R.id.img_three);
//        tvThree.setOnClickListener(onClickListener);
//        tvFour = parent.findViewById(R.id.tv_four);
        imgFour = parent.findViewById(R.id.img_four);
//        tvFour.setOnClickListener(onClickListener);
//        tvFive = parent.findViewById(R.id.tv_five);
        imgFive = parent.findViewById(R.id.img_five);
//        tvFive.setOnClickListener(onClickListener);
//        tvSix = parent.findViewById(R.id.tv_six);
        imgSix = parent.findViewById(R.id.img_six);
//        tvSix.setOnClickListener(onClickListener);
//        tvSeven = parent.findViewById(R.id.tv_seven);
        imgSeven = parent.findViewById(R.id.img_seven);
//        tvSeven.setOnClickListener(onClickListener);
//        tvEight = parent.findViewById(R.id.tv_eight);
        imgEight = parent.findViewById(R.id.img_eight);
//        tvEight.setOnClickListener(onClickListener);
//        tvNine = parent.findViewById(R.id.tv_nine);
        imgNine = parent.findViewById(R.id.img_nine);
//        tvNine.setOnClickListener(onClickListener);
    }
    
    private void initData(){
        started = false;
        gameOver = 0;
        chessData = new int[3][3];
        chessViews = new ImageView[3][3];
//        chessViews[0][0] = tvOne;
//        chessViews[1][0] = tvTwo;
//        chessViews[2][0] = tvThree;
//        chessViews[0][1] = tvFour;
//        chessViews[1][1] = tvFive;
//        chessViews[2][1] = tvSix;
//        chessViews[0][2] = tvSeven;
//        chessViews[1][2] = tvEight;
//        chessViews[2][2] = tvNine;
        chessViews[0][0] = imgOne;
        chessViews[1][0] = imgTwo;
        chessViews[2][0] = imgThree;
        chessViews[0][1] = imgFour;
        chessViews[1][1] = imgFive;
        chessViews[2][1] = imgSix;
        chessViews[0][2] = imgSeven;
        chessViews[1][2] = imgEight;
        chessViews[2][2] = imgNine;
    }

    private void setChessData(){
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                chessData[i][j] = 0;
            }
        }

        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++) {
                chessViews[i][j].setImageDrawable(null);
                final int finalI = i;
                final int finalJ = j;
                chessViews[i][j].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(chessData[finalI][finalJ] == 0){
                            if(mode == 0){
                                userPlay(finalI, finalJ);
                            }else {
                                if(playing == 0){
                                    userPlay(finalI, finalJ);
                                }else {
                                    friendsPlay(finalI, finalJ);
                                }
                            }
                        }
                    }
                });
            }
        }
    }
    
    public void start(){
        started = true;
        setChessData();
        setChessViewsEnable(true);
        if(playing == 1 && mode == 0){
            aiPlay();
        }
    }

    private void gameOver(){
        gameOver = isGameOver();
        started = false;
        setChessViewsEnable(false);
        if(jingChessListener != null){
            jingChessListener.gameOver(gameOver, chessData);
        }
    }

    /**
     * 外部设置
     * @param playing
     */
    public void setPlaying(int playing) {
        if(started){
            try {
                throw new Exception("游戏已经开始");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            this.playing = playing;
            playingChanged(true);
        }
    }

    private void playingChanged(boolean hasChanged){
        if(!hasChanged){
            playing = playing == 0 ? 1 : 0;
        }
        if(jingChessListener != null){
            jingChessListener.playingChange(playing);
        }
    }

    public void setJingChessListener(JingChessListener jingChessListener) {
        this.jingChessListener = jingChessListener;
    }

    /**
     * 设置棋子是否可以点击
     * @param enable
     */
    private void setChessViewsEnable(boolean enable){
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                chessViews[i][j].setEnabled(enable);
            }
        }
    }

    /**
     * 0 : 没有结束 1 : user赢了 2 ：ai或者friends赢了 3 ：平局
     * @return
     */
    public int isGameOver(){
        if(isUserWin()){
            return 1;
        }else if(isAIWin()){
            return 2;
        }else if(isDraw()){
            return 3;
        }
        return 0;
    }

    /**
     * ai是否赢了
     * @return
     */
    private boolean isUserWin(){
        for(int i = 0; i < 3; i++){
            if(chessData[i][0] == 1 && chessData[i][1] == 1 && chessData[i][2] == 1){
                return true;
            }
            if(chessData[0][i] == 1 && chessData[1][i] == 1 && chessData[2][i] == 1){
                return true;
            }
        }
        if(chessData[0][0] == 1 && chessData[1][1] == 1 && chessData[2][2] == 1){
            return true;
        }
        if(chessData[0][2] == 1 && chessData[1][1] == 1 && chessData[2][0] == 1){
            return true;
        }
        return false;
    }

    /**
     * 用户是否赢了
     * @return
     */
    private boolean isAIWin(){
        for(int i = 0; i < 3; i++){
            if(chessData[i][0] == 2 && chessData[i][1] == 2 && chessData[i][2] == 2){
                return true;
            }
            if(chessData[0][i] == 2 && chessData[1][i] == 2 && chessData[2][i] == 2){
                return true;
            }
        }
        if(chessData[0][0] == 2 && chessData[1][1] == 2 && chessData[2][2] == 2){
            return true;
        }
        if(chessData[0][2] == 2 && chessData[1][1] == 2 && chessData[2][0] == 2){
            return true;
        }
        return false;
    }

    /**
     * 是否平局
     * @return
     */
    private boolean isDraw(){
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                if(chessData[i][j] == 0){
                    return false;
                }

            }
        }
        return true;
    }

    /**
     * ai走棋
     */
    private void aiPlay(){
        int[] ints = inspectHasAITwoChessman();
        if((ints[0] == -1) && (ints[1] == -1)){
            ints = inspectHasUserTwoChessman();
            if((ints[0] == -1) && (ints[1] == -1)){
                if(chessData[1][1] == 0){
                    ints = new int[]{1, 1};
                }else {
                    ints = inspectStrategyOne();
                    if((ints[0] == -1) && (ints[1] == -1)){
                        ints = inspectStrategyTwo();
                        if((ints[0] == -1) && (ints[1] == -1)){
                            ints = inspectStrategyThree();
                            if((ints[0] == -1) && (ints[1] == -1)){
                                for(int i = 0; i < 3; i++){
                                    boolean has = false;
                                    for(int j = 0; j < 3; j++){
                                        if(chessData[i][j] == 0){
                                            ints = new int[]{i, j};
                                            has = true;
                                            break;
                                        }
                                    }
                                    if(has){
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        chessData[ints[0]][ints[1]] = 2;
        StringBuffer stringBuffer = new StringBuffer("\n");
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                stringBuffer.append(chessData[i][j]+",");
            }
            stringBuffer.append("\n");
        }
        if(selectChessman == 0){
//            chessViews[ints[0]][ints[1]].setText("2");
            chessViews[ints[0]][ints[1]].setImageResource(R.mipmap.btn_select);
        }else {
//            chessViews[ints[0]][ints[1]].setText("1");
            chessViews[ints[0]][ints[1]].setImageResource(R.mipmap.btn_normal);
        }
        if(isGameOver() == 0){
//            修改执行方
            playingChanged(false);
        }else {
            gameOver();
        }
    }

    private void userPlay(int col, int row){

//        执行用户走棋
        chessData[col][row] = 1;
        StringBuffer stringBuffer = new StringBuffer("\n");
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                stringBuffer.append(chessData[i][j]+",");
            }
            stringBuffer.append("\n");
        }
        if(selectChessman == 0){
//            chessViews[col][row].setText("1");
            chessViews[col][row].setImageResource(R.mipmap.btn_normal);
        }else {
//            chessViews[col][row].setText("2");
            chessViews[col][row].setImageResource(R.mipmap.btn_select);
        }
//        判断走棋后是否结束游戏
        if(isGameOver() == 0){
//            没有结束的话
//            修改执行方
            playingChanged(false);
//            ，判断是否是ai
            if(mode == 0){
//                ai执子走棋
                aiPlay();
            }
        }else {
            gameOver();
        }
    }

    private void friendsPlay(int col, int row){
//        执行friends走棋
        chessData[col][row] = 2;
        if(selectChessman == 0){
//            chessViews[col][row].setText("2");
            chessViews[col][row].setImageResource(R.mipmap.btn_select);
        }else {
//            chessViews[col][row].setText("1");
            chessViews[col][row].setImageResource(R.mipmap.btn_normal);
        }
//        判断走棋后是否结束游戏
        if(isGameOver() == 0){
//            没有结束的话，
//            修改执子方
            playingChanged(false);
        }else {
            gameOver();
        }
    }

    private int[] inspectHasAITwoChessman(){
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                if(hasAITwoChessman(i, j)){
                    return new int[]{i, j};
                }
            }
        }
        return new int[]{-1, -1};
    }

    private int[] inspectHasUserTwoChessman(){
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                if(hasUserTwoChessman(i, j)){
                    return new int[]{i, j};
                }
            }
        }
        return new int[]{-1, -1};
    }

    private int[] inspectStrategyOne(){
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                if(strategy_one(i, j)){
                    return new int[]{i, j};
                }
            }
        }
        return new int[]{-1, -1};
    }

    private int[] inspectStrategyTwo(){
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                if(strategy_two(i, j)){
                    return new int[]{i, j};
                }
            }
        }
        return new int[]{-1, -1};
    }

    private int[] inspectStrategyThree(){
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                if(strategy_three(i, j)){
                    return new int[]{i, j};
                }
            }
        }
        return new int[]{-1, -1};
    }

    private boolean hasUserTwoChessman(int x, int y){
        if(chessData[x][y] == 1 || chessData[x][y] == 2){
            return false;
        }
//        横向
        if(chessData[x][0] == 0 && chessData[x][1] == 1 && chessData[x][2] == 1){
            return true;
        }else if(chessData[x][0] == 1 && chessData[x][1] == 0 && chessData[x][2] == 1){
            return true;
        }else if(chessData[x][0] == 1 && chessData[x][1] == 1 && chessData[x][2] == 0){
            return true;
        }
        //        纵向

        if(chessData[0][y] == 0 && chessData[1][y] == 1 && chessData[2][y] == 1){
            return true;
        } else if(chessData[0][y] == 1 && chessData[1][y] == 0 && chessData[2][y] == 1){
            return true;
        } else if(chessData[0][y] == 1 && chessData[1][y] == 1 && chessData[2][y] == 0){
            return true;
        }

//        右条斜线上的点

        if(chessData[0][0] == 0 && chessData[1][1] == 1 && chessData[2][2] == 1 && x == 0 && y == 0){
            return true;
        } else if(chessData[0][0] == 1 && chessData[1][1] == 0 && chessData[2][2] == 1 && x == 1 && y == 1){
            return true;
        } else if(chessData[0][0] == 1 && chessData[1][1] == 1 && chessData[2][2] == 0 && x == 2 && y == 2){
            return true;
        }

//        左斜线上的点

        if(chessData[0][2] == 0 && chessData[1][1] == 1 && chessData[2][0] == 1 && x == 0 && y == 2){
            return true;
        } else if(chessData[0][2] == 1 && chessData[1][1] == 0 && chessData[2][0] == 1 && x == 1 && y == 1){
            return true;
        } else if(chessData[0][2] == 1 && chessData[1][1] == 1 && chessData[2][0] == 0 && x == 2 && y == 0){
            return true;
        }

        return false;
    }

    private boolean hasAITwoChessman(int x, int y){
        if(chessData[x][y] == 1 || chessData[x][y] == 2){
            return false;
        }
//        横向
        if(chessData[x][0] == 0 && chessData[x][1] == 2 && chessData[x][2] == 2){
            return true;
        }else if(chessData[x][0] == 2 && chessData[x][1] == 0 && chessData[x][2] == 2){
            return true;
        }else if(chessData[x][0] == 2 && chessData[x][1] == 2 && chessData[x][2] == 0){
            return true;
        }
        //        纵向

        if(chessData[0][y] == 0 && chessData[1][y] == 2 && chessData[2][y] == 2){
            return true;
        } else if(chessData[0][y] == 2 && chessData[1][y] == 0 && chessData[2][y] == 2){
            return true;
        } else if(chessData[0][y] == 2 && chessData[1][y] == 2 && chessData[2][y] == 0){
            return true;
        }

//        右条斜线上的点

        if(chessData[0][0] == 0 && chessData[1][1] == 2 && chessData[2][2] == 2 && x == 0 && y == 0){
            return true;
        } else if(chessData[0][0] == 2 && chessData[1][1] == 0 && chessData[2][2] == 2 && x == 1 && y == 1){
            return true;
        } else if(chessData[0][0] == 2 && chessData[1][1] == 2 && chessData[2][2] == 0 && x == 2 && y == 2){
            return true;
        }

//        左斜线上的点

        if(chessData[0][2] == 0 && chessData[1][1] == 2 && chessData[2][0] == 2 && x == 0 && y == 2){
            return true;
        } else if(chessData[0][2] == 2 && chessData[1][1] == 0 && chessData[2][0] == 2 && x == 1 && y == 1){
            return true;
        } else if(chessData[0][2] == 2 && chessData[1][1] == 2 && chessData[2][0] == 0 && x == 2 && y == 0){
            return true;
        }

        return false;
    }

    /**
     * 某条线仅仅有己方棋子一枚
     */
    private boolean strategy_one(int x, int y){
        if(chessData[x][y] == 1 || chessData[x][y] == 2){
            return false;
        }
//        横向

        if(chessData[x][0] == 0 && chessData[x][1] == 0 && chessData[x][2] == 1 && y != 2){
            return true;
        } else if(chessData[x][0] == 0 && chessData[x][1] == 1 && chessData[x][2] == 0 && y != 1){
            return true;
        } else if(chessData[x][0] == 1 && chessData[x][1] == 0 && chessData[x][2] == 0 && y != 0){
            return true;
        }

//        纵向

        if(chessData[0][y] == 0 && chessData[1][y] == 0 && chessData[2][y] == 1  && x != 2){
            return true;
        } else if(chessData[0][y] == 0 && chessData[1][y] == 1 && chessData[2][y] == 0  && x != 1){
            return true;
        } else if(chessData[0][y] == 1 && chessData[1][y] == 0 && chessData[2][y] == 0  && x != 0){
            return true;
        }

//        右条斜线上的点

        if(chessData[0][0] == 0 && chessData[1][1] == 0 && chessData[2][2] == 1 && x != 2 && y != 2){
            return true;
        } else if(chessData[0][0] == 0 && chessData[1][1] == 1 && chessData[2][2] == 0 && x != 1 && y != 1){
            return true;
        } else if(chessData[0][0] == 1 && chessData[1][1] == 0 && chessData[2][2] == 0 && x != 0 && y != 0){
            return true;
        }

//        左斜线上的点

        if(chessData[0][2] == 0 && chessData[1][1] == 0 && chessData[2][0] == 1 && x != 2 && y != 0){
            return true;
        } else if(chessData[0][2] == 0 && chessData[1][1] == 1 && chessData[2][0] == 0 && x != 1 && y != 1){
            return true;
        } else if(chessData[0][2] == 1 && chessData[1][1] == 0 && chessData[2][0] == 0 && x != 0 && y != 2){
            return true;
        }

//        非斜线上的点
        if(!(x == 1 && y == 1) && x == 1 || y == 1){
            return false;
        }
        return false;
    }

    /**
     * 某条线仅仅有敌方棋子一枚
     */
    private boolean strategy_two(int x, int y){
//        选中的点不能为有棋子的
        if(chessData[x][y] == 2){
            return false;
        }
//        横向

        if(chessData[x][0] == 0 && chessData[x][1] == 0 && chessData[x][2] == 2){
            return true;
        } else if(chessData[x][0] == 0 && chessData[x][1] == 2 && chessData[x][2] == 0){
            return true;
        } else if(chessData[x][0] == 2 && chessData[x][1] == 0 && chessData[x][2] == 0){
            return true;
        }

//        纵向

        if(chessData[0][y] == 0 && chessData[1][y] == 0 && chessData[2][y] == 2){
            return true;
        } else if(chessData[0][y] == 0 && chessData[1][y] == 2 && chessData[2][y] == 0){
            return true;
        } else if(chessData[0][y] == 2 && chessData[1][y] == 0 && chessData[2][y] == 0){
            return true;
        }

//        右条斜线上的点
        if(x == 0 && y == 0 || (x == 1 && y == 1) || (x == 2 && y == 2)){
            if(chessData[0][0] == 0 && chessData[1][1] == 0 && chessData[2][2] == 2){
                return true;
            } else if(chessData[0][0] == 0 && chessData[1][1] == 2 && chessData[2][2] == 0){
                return true;
            } else if(chessData[0][0] == 2 && chessData[1][1] == 0 && chessData[2][2] == 0){
                return true;
            }
        }
//        左斜线上的点
        if(x == 2 && y == 0 || (x == 1 && y == 1) || (x == 0 && y == 2)) {
            if(chessData[0][2] == 0 && chessData[1][1] == 0 && chessData[2][0] == 2){
                return true;
            } else if(chessData[0][2] == 0 && chessData[1][1] == 2 && chessData[2][0] == 0){
                return true;
            } else if(chessData[0][2] == 2 && chessData[1][1] == 0 && chessData[2][0] == 0){
                return true;
            }
        }
//        非斜线上的点
        if(!(x == 1 && y == 1) && x == 1 || y == 1){
            return false;
        }
        return false;
    }

    /**
     * 某条线上空空如也
     */
    private boolean strategy_three(int x, int y){
//        横向
        if(chessData[x][0] == 0 && chessData[x][1] == 0 && chessData[x][2] == 0){
            return true;
        }
//        纵向
        if(chessData[0][y] == 0 && chessData[1][y] == 0 && chessData[2][y] == 0){
            return true;
        }
//        右条斜线上的点
        if(chessData[0][0] == 0 && chessData[1][1] == 0 && chessData[2][2] == 0){
            return true;
        }
//        左斜线上的点
        if(chessData[0][2] == 0 && chessData[1][1] == 0 && chessData[2][0] == 0){
            return true;
        }
//        非斜线上的点
        if(!(x == 1 && y == 1) && x == 1 || y == 1){
            return false;
        }
        return false;
    }

    public interface JingChessListener{
        /**
         * 执子方发生改变
         * @param playing 0 ： user 1 ： ai或者friends
         */
        void playingChange(int playing);

        /**
         * 游戏结束
         * @param gameOver 1 ：user赢了 2 ： ai或者friends赢了 3 ：平局
         * @param chessData 游戏的棋局
         */
        void gameOver(int gameOver, int[][] chessData);
    }
}
